<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: add.phtml 3032 2010-05-25 08:00:42Z huuphuoc $
 */
?>
<?php
$this->headTitle($this->translator('product_add_page_title'));
$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js')
    ->appendFile($this->APP_STATIC_SERVER . '/js/tomato/util.js');
?>

<style>
    .box_image{
        position:relative; float:left; width:160px; padding-top:5px;
    }
    .box_image a{
        left:133px; top:2px; position:absolute; color:
    }
</style>

<div class="grid_12 t_a_ui_breadcrumb">
    <span><a href="<?php echo $this->url(array(), 'shop_product_list'); ?>"><?php echo $this->translator('product_list_breadcump'); ?></a></span>
    <span><?php echo $this->translator('product_add_breadcump'); ?></span>
</div>

<div class="grid_12"><?php echo $this->flashMessenger(); ?></div>

<form method="post" action="<?php echo $this->url(array(), 'shop_product_add'); ?>" id="addProductForm">
<div class="grid_12 t_a_bottom"><?php echo $this->translator('product_add_guide'); ?></div>

<div class="grid_12 t_a_bottom">
    <div class="prefix_1 suffix_1">
        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_admin_name'); ?>:</label>
            <input type="text" id="product_admin_name" name="product_admin_name" style="width: 300px" />
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_status'); ?>:</label>
            <select id="product_status" name="product_status" style="width:170px">
                <option value="">---</option>
                <option value="1"><?php echo $this->translator('product_status_active'); ?></option>
                <option value="0"><?php echo $this->translator('product_status_inactive'); ?></option>
            </select>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_price'); ?>:</label>
            <input type="text" id="product_price" name="product_price" style="width: 170px" /> <?php echo isset($this->enterprise->currency)?$this->enterprise->currency:null;?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_status_of_delivery'); ?>:</label>
            <select id="product_status_of_delivery" name="product_status_of_delivery" style="width:170px">
                <option value="">---</option>
                <option value="deliver_able"><?php echo $this->translator('product_status_of_delivery_deliverable'); ?></option>
                <option value="non_deliver_able"><?php echo $this->translator('product_status_of_delivery_non_deliverable'); ?></option>
                <option value="please_contact"><?php echo $this->translator('product_status_of_delivery_please_contact'); ?></option>
            </select>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_quantity_mode'); ?>:</label>
            <select id="product_quantity_mode" name="product_quantity_mode" style="width:170px" disabled>
                <option value="">---</option>
                <option value="infinity"><?php echo $this->translator('product_quantity_mode_infinity'); ?></option>
                <option value="amount"><?php echo $this->translator('product_quantity_mode_amount'); ?></option>
            </select>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_quantity'); ?>:</label>
            <input type="text" disabled id="product_quantity" name="product_quantity" style="width: 170px" />
        </div>

        <hr/>
		
        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_image'); ?>:</label>
			<div class="clearfix" id="uploadFile">
				<?php
					echo $this->helperLoader('upload')->uploader('multimedia',
						array('extension' => '*.jpg;*.jpeg;*.png;*.gif', 'multi' => true, 'auto' => true, 'simUploadLimit' => 5, 						'sizeLimit' => 1024 * 1024 * 5),
						array('onProgress' => 'onUploadProgress', 'onComplete' => 'onUploadComplete'),
						'uploadFiles');					
				?>
				<div style="padding-left: 160px;">
					<?php 
						echo $this->browser('/upload/multimedia/'.Zend_Auth::getInstance()->getIdentity()->user_name, 'jpg,gif,png', 'insertImage(%s)');
					?>
				</div>
				<div id="viewIMG" style="padding-left:160px"></div>	
				<input type="hidden" id="product_image" name="product_image" value="" />
				<input type="hidden" value="1" id="hd_num_image" />				
			</div>
			<script language="javascript">
				function onUploadComplete(event, ID, fileObj, response, data) {
				};			
			</script>
        </div>
		
        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_created_date'); ?>:</label>
            <?php echo $this->date;?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_modified_date'); ?>:</label>
            <?php echo $this->date;?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_category'); ?>:</label>
            <select name="category_id[]" id="category_id[]" style="width:170px;">
                <option value="">---</option>
                <?php for ($i = 0; $i < count($this->categories); $i++) : ?>
                <option value="<?php echo $this->categories[$i]->category_id; ?>"><?php echo str_repeat('---', $this->categories[$i]->depth).$this->categories[$i]->category_admin_name; ?></option>
            <?php endfor; ?>
            </select>
            <?php /*<div style="border:1px solid #CCC; padding: 5px; width:300px;overflow:auto;height:120px">
                <?php for ($i = 0; $i < count($this->categories); $i++) : ?>
                <input class="category_checkbox" type="checkbox" id="category_id[]" name="category_id[]" value="<?php echo $this->categories[$i]->category_id; ?>" <?php if($this->categories[$i]->category_enable_product != 1):?>disabled <?php endif;?> > <?php echo str_repeat('---', $this->categories[$i]->depth).$this->categories[$i]->category_admin_name; ?><br>
                <?php endfor; ?>
            </div>*/?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_bundle'); ?>:</label>
            <input type="radio" class="product_bundle" name="product_bundle" value="1"> Yes
            <input type="radio" class="product_bundle" name="product_bundle" value="0" checked="checked"> No<br />
            <div id="html_products" class="clearfix t_a_bottom" style="margin-left:20%;"> 
				<?php echo $this->translator('product_bundle_choice'); ?>
			</div>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_shipping_price_1'); ?>:</label>
            <input type="text" name="product_shipping_price" id="product_shipping_price" style="width:170px;"> <?php echo isset($this->enterprise->currency)?$this->enterprise->currency:null;?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('product_shipping_price'); ?>:</label>
            <input type="text" name="product_shipping_price_1" id="product_shipping_price_1" style="width:50px;"> +
            <span><?php echo $this->translator('product_quantity');?></span> x
            <input type="text" name="product_shipping_price_2" id="product_shipping_price_2" style="width:50px;">
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label>&nbsp;</label>
            <button type="submit"><span><?php echo $this->translator('product_add_save_button'); ?></span></button>&nbsp;&nbsp;&nbsp;
            <button type="reset"><span><?php echo $this->translator('product_reset_button'); ?></span></button>
        </div>
    </div>
</div>
</form>

<script type="text/javascript">
function insertImage(url) {
    num_image = jQuery('#hd_num_image').val();
    if(num_image >= 7){
        alert('<?php echo $this->translator('product_upload_full_image');?>');
    }else{
        htmlVal = document.getElementById('viewIMG').innerHTML;
        new_str_image = '<span class="box_image" id="'+num_image+'"><a href="javascript:void(0)" class="removeImage" onClick="removeImage(this);" rel="'+num_image+'" val="'+url+'">[x]</a><img src="'+url+'" width="150"/></span>'+htmlVal;
        document.getElementById('viewIMG').innerHTML = new_str_image;

        val = document.getElementById('product_image').value;
        document.getElementById('product_image').value = url+','+val;

        /**
        * Update num image
        */
        jQuery('#hd_num_image').attr('value', parseInt(num_image)+1);
    }
};

function removeImage(your) {
    rel = jQuery(your).attr('rel');
    jQuery('#'+rel).html('');
    jQuery('#'+rel).remove();

    str_image = jQuery('#product_image').val();
    val = jQuery(your).attr('val');
    new_str_image = str_image.replace(val+',','');
    jQuery('#product_image').attr('value',new_str_image);

    /**
    * Update num image
    */
    num_image = jQuery('#hd_num_image').val();
    jQuery('#hd_num_image').attr('value', parseInt(num_image)-1);
};

$(document).ready(function() {

    $('.product_bundle').click(function(){
        $('.product_bundle:checked').each(function()    {
            if ($(this).val()== '1')
            {
                document.getElementById('viewIMG').innerHTML = '';
                $("#product_image").attr("value", "");

                $('#html_products').html('Loading...');
                $.ajaxq('product',{
                    url: "<?php echo $this->url(array(), 'shop_product_list_bundle'); ?>",
                    method : "POST",
                    data: {value : $(this).val(), product_product_id_chain : ','},
                    success: function(html){
                        $('#html_products').html(html);
                    }
                });
            }
            else if ($(this).val()== '0')
            {
                $('#html_products').html('Please! Click to choice radio button...');
            }
            else
            {
                alert('789');
            }
        });
    });

    /*$('.category_checkbox').bind('click', function(){
        var strCategory_id = '';
        $('.category_checkbox:checked').each(function()    {
            var value = $(this).val();
            strCategory_id += value+',';
        });
        $.ajax({
            url: "<?php echo $this->url(array(), 'shop_product_get_attribute'); ?>",
            method : "POST",
            data: {strCategory_id : strCategory_id},
            success: function(html){
                $('#attributeList').html(html);
            }
        });
    });*/

    $('#addProductForm').validate({
        rules: {
            product_admin_name: {
                required: true,
                maxlength: 255
            },
            product_status:{
                required: true
            },
            product_status_of_delivery:{
                required: true
            },
            product_quantity_mode:{
                required: true
            },
            product_quantity:{
                required: true,
                number:true
            },
            product_price:{
                number: true
            },
            "category_id[]": "required",
            product_shipping_price:{
                number:true
            },
            product_shipping_price_1:{
                number:true
            },
            product_shipping_price_2:{
                number:true
            }
        },
        messages: {
            product_admin_name: {
                required: "<?php echo $this->translator('product_name_required'); ?>",
                maxlength: "<?php echo $this->translator('product_name_maxlength'); ?>"
            },
            product_status: "<?php echo $this->translator('product_status_required'); ?>",
            product_status_of_delivery: "<?php echo $this->translator('product_status_of_delivery_required'); ?>",
            product_quantity_mode: "<?php echo $this->translator('product_quantity_mode_required'); ?>",
            product_quantity: {
                required: "<?php echo $this->translator('product_quantity_required'); ?>",
                number: "<?php echo $this->translator('product_quantity_invalid_number'); ?>"
            },
            product_price: "<?php echo $this->translator('product_price_number'); ?>",
            "category_id[]": "<?php echo $this->translator('product_category_id_required'); ?>",
            product_shipping_price:'<?php echo $this->translator('product_shipping_price_1_invalid_number'); ?>',
            product_shipping_price_1:'<?php echo $this->translator('product_shipping_price_1_invalid_number'); ?>',
            product_shipping_price_2:'<?php echo $this->translator('product_shipping_price_1_invalid_number'); ?>'
        }
    });

    $('#product_status_of_delivery').bind('change', function(){
        var value = $(this).val();
        if(value == 'deliver_able'){
            $('#product_quantity_mode').removeAttr('disabled');
            $('#product_quantity_mode').attr('value', '');

            $('#product_quantity_mode').bind('change', function(){
                var value_mode = $('#product_quantity_mode').val();
                if(value_mode == "infinity"){
                    $('#product_quantity').attr('disabled', 'disabled');
                    $('#product_quantity').attr('value', '');
                }
                else if(value_mode == "amount"){
                    $('#product_quantity').attr('disabled', '');
                    $('#product_quantity').attr('value', '');
                }
                else{
                    $('#product_quantity').attr('disabled', 'disabled');
                    $('#product_quantity').attr('value', '');
                }
            });
        }
        else if(value == 'non_deliver_able'){
            $('#product_quantity_mode').attr('disabled', 'disabled');
            $('#product_quantity_mode').attr('value', '');
            $('#product_quantity').attr('disabled', 'disabled');
            $('#product_quantity').attr('value', '');
        }
        else if(value == 'please_contact'){
			/*
				Name    : Cong Ngo, Date time: 09:17 26-4-2011
				purpose : fix bug when user click please contact tows fields product_quantity and product_quantity_mode have must is disable
			*/
			$('#product_quantity_mode').attr('value', '');
            $('#product_quantity_mode').attr('disabled', 'disabled');            
			$('#product_quantity').attr('value', '');
            $('#product_quantity').attr('disabled', 'disabled');            			
			/*
				End Edit
			*/
        }
        else{
            $('#product_quantity_mode').attr('disabled', 'disabled');
            $('#product_quantity_mode').attr('value', '');
            $('#product_quantity').attr('disabled', 'disabled');
            $('#product_quantity').attr('value', '');
        }
    });
});
</script>
